package com.atguigu.tingshu.listener;

import com.atguigu.tingshu.model.user.UserInfo;
import io.xzxj.canal.core.annotation.CanalListener;
import io.xzxj.canal.core.listener.EntryListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

@Slf4j
@CanalListener(destination = "tingshuTopic", schemaName = "tingshu_user", tableName = "user_info")
public class UserListener implements EntryListener<UserInfo> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 监听表数据新增
     *
     * @param userInfo 数据库新增的数据
     */
    @Override
    public void insert(UserInfo userInfo) {
        log.info("监听表数据新增：{}", userInfo);
    }

    /**
     * 监听表数据新增
     *
     * @param before 修改之前的数据
     * @param after  修改之后的数据
     * @param fields 更新的属性集合
     */
    @Override
    public void update(UserInfo before, UserInfo after, Set<String> fields) {
        log.info("监听表数据修改：before:{},after:{}", before, after);
        String redisKey = "user:info:"+after.getId();
        redisTemplate.delete(redisKey);
    }

    /**
     * 监听表数据新增
     *
     * @param userInfo 数据库删除的数据
     */
    @Override
    public void delete(UserInfo userInfo) {
        log.info("监听表数据删除：{}", userInfo);
    }
}
